﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BCL;
using System.IO;
using UploadingControl.SilverlightService;
using System.Collections.ObjectModel;

namespace UploadingControl
{
    public partial class NewsDetailPage : UserControl
    {
        PageMode _Mode;
        LogoInformation _logoInformation;
        public NewsDetailPage(PageMode pageMode, LogoInformation logoinformation)
        {
            InitializeComponent();
            _logoInformation = logoinformation;
            CheckDate();
            SetValue("");
            GetMenu();
            _Mode = pageMode;
            if (pageMode == PageMode.Edit)
            {
                datePicker1.IsEnabled = false;
                datePicker1.SelectedDate = logoinformation.CurrentDate;
                fileFarman.CompleteUploadedURL = logoinformation.FarmanImage;
                fileHeader.CompleteUploadedURL = logoinformation.HeaderImage;
                fileLog.CompleteUploadedURL = logoinformation.LogoImage;
                fileFarman.Finalize();
                fileHeader.Finalize();
                fileLog.Finalize();
            }
        }
        public List<LookUp> lstLookUp { get; set; }
        public void GetMenu()
        {
            SilverlightServiceClient client = new SilverlightServiceClient();
            client.GetMenuCompleted += (se, arg) =>
                {
                    lstLookUp = arg.Result;
                    lstMenu.DataContext = this;
                };

            client.GetMenuAsync();
        }

        private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
        {
            if (datePicker1.SelectedDate != null)
            {
                HyperlinkButton hyper = sender as HyperlinkButton;
                if (_logoInformation == null)
                {
                    _logoInformation = new LogoInformation();
                    _logoInformation.Detail = new List<LogoDetail>();
                }
                var menu = lstLookUp.Where(f => f.LookUpID == Convert.ToInt32(hyper.Tag)).FirstOrDefault();
                var isMenuExist = _logoInformation.Detail.Where(f => f.MenuId == menu.LookUpID).FirstOrDefault();
                if (isMenuExist == null)
                {
                    isMenuExist = new LogoDetail();
                    isMenuExist.MenuId = menu.LookUpID;
                    isMenuExist.MenuName = menu.Description;
                }
                NewDetailPopup popup = new NewDetailPopup(_Mode, isMenuExist, datePicker1.SelectedDate.Value.ToShortDateString().Replace("/", "-"));

                popup.Closing += (s, d) =>
                    {
                        if (popup.DialogResult != null && popup.DialogResult.Value)
                        {
                            if (_Mode == PageMode.Edit)
                            {
                                // remvoe the existing detail info from list
                                _logoInformation.Detail.Remove(_logoInformation.Detail.Where(x => x.Id == popup.LogoDetail.Id).FirstOrDefault());
                            }


                            _logoInformation.Detail.Add(popup.LogoDetail);

                        }
                    };
                popup.Show();
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            CheckDate();

        }

        private void CheckDate()
        {
            if (datePicker1.SelectedDate != null)
            {
                fileFarman.isUploadFile = true;
                fileHeader.isUploadFile = true;
                fileLog.isUploadFile = true;
                App.TodayDate = datePicker1.SelectedDate.Value;
                SetValue(datePicker1.SelectedDate.Value.ToShortDateString().Replace("/", "-"));
            }
            else
            {
                fileFarman.isUploadFile = false;
                fileHeader.isUploadFile = false;
                fileLog.isUploadFile = false;

            }
        }

        private void SetValue(string date)
        {
            fileFarman.Initialize("/documents/Farman", "Farman" + date, "Farman");
            fileHeader.Initialize("/documents/Header", "Header" + date, "Header");
            fileLog.Initialize("/documents/Logo", "Logo" + date, "Logo");
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            SilverlightServiceClient client = new SilverlightServiceClient();
            _logoInformation.FarmanImage = fileFarman.CompleteUploadedURL;
            _logoInformation.LogoImage = fileLog.CompleteUploadedURL;
            _logoInformation.HeaderImage = fileHeader.CompleteUploadedURL;
            client.SaveCompleted += (se, arg) =>
                {
                    if (arg.Error == null)
                    {
                        OpenListPage();
                    }

                };
            _logoInformation.CreatedBy = App.UserId;
            _logoInformation.UpdatedBy = App.UserId;
            if (datePicker1.SelectedDate.HasValue)
            {
                client.AlreadyExistCompleted += (se, arg) =>
                    {
                        if (arg.Result)
                        {
                            _logoInformation.CurrentDate = datePicker1.SelectedDate.Value;
                            client.SaveAsync(_logoInformation);
                        }
                        else
                        {
                            MessageBox.Show("Selected date already exist", "Error", MessageBoxButton.OK);
                        }
                    };
                if (_Mode == PageMode.Add)
                    client.AlreadyExistAsync(datePicker1.SelectedDate.Value);
                else if (_Mode == PageMode.Edit)
                {
                    _logoInformation.CurrentDate = datePicker1.SelectedDate.Value;
                    client.SaveAsync(_logoInformation);
                }


            }
            else
            {
                MessageBox.Show("Please select date");
            }
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            OpenListPage();
        }
        void OpenListPage()
        {
            StackPanel panel = ((StackPanel)this.Parent);
            panel.Children.Clear();
            panel.Children.Add(new NewListPage());
        }


    }
}
